import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/ToastUtil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:magic_car/main.dart';
import 'package:toast/toast.dart';

class UserPage extends StatefulWidget {
  UserPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<UserPage> {
  Map userinfo = new Map();
  @override
  void initState() {
    super.initState();
    loaduserinfo();
  }

  loaduserinfo() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    Map<String, String> map = new Map();
    map["id"] = pref.getString('uid');

    HttpHelper.post(HttpHelper.API_Userinfo, (data) async {
      if (data != null) {
        print(data);
        Toast.show(data["msg"], context);

        if (data["code"].toString() == "1") {
          setState(() {
            userinfo = data["info"];
          });
          //pref.setStringList('userinfo', data["info"]);

        }
      }
    }, params: map);
  }

  Widget build(BuildContext context) {
    return new MaterialApp(
        title: '',
        home: new Scaffold(
            appBar: new AppBar(
                title: new Text(''),
                backgroundColor: Color.fromRGBO(255, 204, 48, 1),
                elevation: 0),
            body:
                //     SingleChildScrollView(
                //       scrollDirection: Axis.vertical,
                // //true 滑动到底部
                // reverse: false,
                // padding: EdgeInsets.all(0.0),
                // physics: BouncingScrollPhysics(),
                // child:
                ConstrainedBox(
                    constraints: BoxConstraints.expand(),
                    child: new ListView(children: <Widget>[
                      Stack(alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
                          children: <Widget>[
                            Positioned(
                              top: 0,
                              left: 0,
                              right: 0,
                              // child:
                              // Center(
                              child: new Container(
                                  color: Color.fromRGBO(255, 204, 48, 1),
                                  height: 200,
                                  // width: double.infinity,
                                  child: Text('')),

                              // child: new SizedBox(
                              //   width: double.infinity,
                              //   height: 200,
                              //   child: new Container(
                              //     color: Color.fromRGBO(255, 204, 48, 1),
                              //     child: Text('111111121323232'),
                              //   ),
                              // ),
                              // child: Container(
                              //   height: 200,
                              //   color: Color.fromRGBO(255, 204, 48, 1),
                              // ),
                            ),
                            Positioned(
                              top: 80.0,
                              left: 20,
                              right: 20,
                              child: new Container(
                                height: 180,
                                decoration: new BoxDecoration(
                                    color: Colors.white,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(5)),
                                    border: new Border.all(
                                        width: 0, color: Colors.white)),
                                child: Column(
                                  children: <Widget>[
                                    Padding(
                                      padding:
                                          new EdgeInsets.fromLTRB(0, 70, 0, 0),
                                      child: Text(
                                        userinfo.length != 0
                                            ? userinfo["name"]
                                            : "用户名",
                                        style: TextStyle(fontSize: 18),
                                      ),
                                    ),
                                    // Padding(
                                    //     padding:
                                    //         new EdgeInsets.fromLTRB(0, 20, 0, 0),
                                    //     child: Row(
                                    //       crossAxisAlignment:
                                    //           CrossAxisAlignment.center,
                                    //       mainAxisAlignment:
                                    //           MainAxisAlignment.center,
                                    //       children: <Widget>[
                                    //         Expanded(
                                    //           flex: 1,
                                    //           child: Align(
                                    //             child: Text('898'),
                                    //             alignment: Alignment.centerRight,
                                    //           ),
                                    //         ),
                                    //         Expanded(
                                    //           flex: 1,
                                    //           child: Text('极好'),
                                    //         )
                                    //       ],
                                    //     )),
                                    // Padding(
                                    //   padding:
                                    //       new EdgeInsets.fromLTRB(0, 5, 0, 0),
                                    //   child: Text(
                                    //     '信用分',
                                    //     style: TextStyle(fontSize: 12),
                                    //   ),
                                    // ),
                                  ],
                                ),
                              ),
                            ),
                            Positioned(
                              top: 30.0,
                              child: Center(
                                  child: Container(
                                width: 100,
                                height: 100,
                                decoration: new BoxDecoration(
                                  //背景
                                  color: Colors.white,
                                  //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(50)),
                                  //设置四周边框
                                  border: new Border.all(
                                      width: 0.5, color: Colors.white),
                                ),
                                child: new Image.asset(
                                    'resource/images/user/user1.png'),
                              )),
                            ),
                            Positioned(
                                top: 380.0,
                                left: 20,
                                right: 20,
                                child: new Container(
                                  height: 50,
                                  decoration: new BoxDecoration(
                                      color: Colors.white,
                                      borderRadius:
                                          BorderRadius.all(Radius.circular(5)),
                                      border: new Border.all(
                                          width: 0, color: Colors.white)),
                                  child: SizedBox(
                                      width: double.infinity,
                                      height: 45,
                                      child: new RaisedButton(
                                        child: Text(
                                          '退出登录',
                                          style: TextStyle(
                                              color: Color.fromRGBO(
                                                  102, 102, 102, 1),
                                              fontSize: 16),
                                        ),
                                        color: Colors.white,
                                        textColor: Colors.black,
                                        onPressed: () async {
                                          SharedPreferences pref =
                                              await SharedPreferences
                                                  .getInstance();
                                          pref.clear();
                                          Navigator.of(context)
                                              .pushAndRemoveUntil(
                                                  new MaterialPageRoute(
                                                      builder: (context) =>
                                                          new MyApp()),
                                                  (route) => route == null);
                                        },
                                      )),
                                )),
                            Positioned(
                              top: 270.0,
                              left: 20,
                              right: 20,
                              child: new Container(
                                height: 101,
                                decoration: new BoxDecoration(
                                    color: Colors.white,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(5)),
                                    border: new Border.all(
                                        width: 0, color: Colors.white)),
                                child: Column(
                                  children: <Widget>[
                                    Padding(
                                        padding: new EdgeInsets.fromLTRB(
                                            15, 0, 15, 0),
                                        child: GestureDetector(
                                          onTap: () {
                                            //检测记录
                                            print('点击检测记录');
                                            Navigator.pushNamed(
                                                context, "jiance_rec");
                                          },
                                          child: Row(
                                            // crossAxisAlignment: CrossAxisAlignment.start,
                                            // mainAxisAlignment: MainAxisAlignment.start,
                                            children: <Widget>[
                                              Expanded(
                                                  flex: 1,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.centerLeft,
                                                    child: Image.asset(
                                                      'resource/images/user/jilu.png',
                                                      height: 50,
                                                    ),
                                                  )),
                                              Expanded(
                                                  flex: 7,
                                                  child: Align(
                                                      alignment:
                                                          Alignment.centerLeft,
                                                      child: Text(
                                                        '检测记录                               ',
                                                        style: TextStyle(
                                                            fontSize: 16,
                                                            color:
                                                                Color.fromRGBO(
                                                                    123,
                                                                    123,
                                                                    123,
                                                                    1)),
                                                      ))),
                                              Expanded(
                                                  flex: 1,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.centerRight,
                                                    child: Image.asset(
                                                      'resource/images/user/detail.png',
                                                      height: 20,
                                                    ),
                                                  ))
                                            ],
                                          ),
                                        )),
                                    Container(
                                      height: 1,
                                      color: Color.fromRGBO(238, 238, 238, 1),
                                    ),
                                    Padding(
                                        padding: new EdgeInsets.fromLTRB(
                                            15, 0, 15, 0),
                                        child: GestureDetector(
                                          onTap: () {
                                            //联系我们
                                            Navigator.pushNamed(
                                                context, "contact");
                                          },
                                          child: Row(
                                            // crossAxisAlignment: CrossAxisAlignment.start,
                                            // mainAxisAlignment: MainAxisAlignment.start,
                                            children: <Widget>[
                                              Expanded(
                                                  flex: 1,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.centerLeft,
                                                    child: Image.asset(
                                                      'resource/images/user/lianxi.png',
                                                      height: 50,
                                                    ),
                                                  )),
                                              Expanded(
                                                  flex: 7,
                                                  child: Align(
                                                      alignment:
                                                          Alignment.centerLeft,
                                                      child: Text(
                                                        '联系我们                                       ',
                                                        style: TextStyle(
                                                            fontSize: 16,
                                                            color:
                                                                Color.fromRGBO(
                                                                    123,
                                                                    123,
                                                                    123,
                                                                    1)),
                                                      ))),
                                              Expanded(
                                                  flex: 1,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.centerRight,
                                                    child: Image.asset(
                                                      'resource/images/user/detail.png',
                                                      height: 20,
                                                    ),
                                                  ))
                                            ],
                                          ),
                                        ))
                                  ],
                                ),
                              ),
                            ),
                          ])
                    ]))));
  }
}
